﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using Presenter_SSResourceManager;
using Model_SSResourceManager;

namespace WebView_SSResourceManager
{
    public partial class Register : System.Web.UI.Page, IViewRegister
    {
        PresenterRegister _presenter;
        double defaultMessageTimeSpan = 15d;

        protected void Page_Load(object sender, EventArgs e)
        {
            _presenter = new PresenterRegister(this);

            txtMessageTimeSpan.Text = defaultMessageTimeSpan.ToString();
            if (Session["TempValue"] != null)
            {
                _presenter.loginExist = (bool)Session["TempValue"];
                Session.Remove("TempValue");
            }
        }

        protected void btnCreateUser_RegisterUser(object sender, EventArgs e)
        {
            _presenter.RegisterUser();

        }

        public void LoginExist(bool loginExist)
        {
            Session.Add("TempValue", loginExist);
            if (loginExist)
            {
                lblErrorMessage.Text = "This login exist";
                lblErrorMessage.ForeColor = Color.Red;
            }
            else
            {
                lblErrorMessage.Text = "This login free";
                lblErrorMessage.ForeColor = Color.Green;
            }
        }

        protected void txtUserName_LoginExist(object sender, EventArgs e)
        {
            var login = txtUserName.Text.Trim();
            _presenter.LoginExist(login);
        }

        public User GetUserInformation(User user)
        {
            user.FirstName = txtFirstName.Text;
            user.LastName = txtLastName.Text;
            user.Email = txtEmail.Text;
            user.Skype = txtSkype.Text;
            user.Login = txtUserName.Text;
            user.Password = txtPassword.Text.GetHashCode();
            user.Status = Model_SSResourceManager.UserStatus.Active;
            var messageTimeSpan = (txtMessageTimeSpan.Text == string.Empty)
                ? defaultMessageTimeSpan
                : double.Parse(txtMessageTimeSpan.Text);
            user.MessageTimeSpan = TimeSpan.FromMinutes(messageTimeSpan);
            user.Online = true;
            return user;
        }



        public void ResultOfRegistration(bool resultValue)
        {
            if (resultValue)
            {
                Response.Redirect("~/Guest/Login.aspx");
            }
            else
            {
                lblMessage.Text = "Sorry, but a user with this login already exists";
            }
        }

    }
}
